package com.gl.servlet.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class EncodingFilter
  implements Filter
{
  private String targetEncoding = "UTF-8";

  public void init(FilterConfig config) throws ServletException {
    this.targetEncoding = config.getInitParameter("encoding");
  }

  public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException
  {
    HttpServletRequest request = (HttpServletRequest)srequest;
    request.setCharacterEncoding(this.targetEncoding);

    chain.doFilter(srequest, sresponse);
  }

  public void destroy() {
    this.targetEncoding = null;
  }
}